﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SupplyBills : MonoBehaviour {

    int m_iTotalPrice;

    public List<WpnCategorize> m_SupplyWpnGroup = new List<WpnCategorize>();
	// Use this for initialization
	void Start () {

        WpnRandomly();
        WpnBox();

        m_iTotalPrice = 0;
        transform.FindChild("TotalPrice").GetComponent<UILabel>().depth = GetComponent<UISprite>().depth+1;
	}
    // Update is called once per frame
    void Update()
    {
	    
	}

    void ResetPage()
    {
        for (int i = 0; i < transform.childCount; ++i)
        {
            if (transform.GetChild(i).gameObject.name == "SupplyWpnBox(Clone)")
            {
                transform.GetChild(i).GetComponent<SupplyWpnBox>().Init();
                if (!transform.GetChild(i).GetComponent<SupplyWpnBox>().m_bExample)
                {
                    transform.GetChild(i).FindChild("SupplyBarPointer").localPosition = new Vector3(-3, 0);
                    transform.GetChild(i).FindChild("SupplyBarPointer").GetComponent<SupplyBarPointer>().BuyingObj_NumCheck();
                }
            }
        }
    }

    void OnClick() // Confirm Buy//////////////
    {
        if (GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().g_iMoney >= m_iTotalPrice)
        {
            for (int i = 0; i < transform.childCount; ++i)
            {
                if (transform.GetChild(i).gameObject.name == "SupplyWpnBox(Clone)")
                {
                    if (transform.GetChild(i).GetComponent<SupplyWpnBox>().m_iNumOfBuyingObj > 0)
                    {
                        for (int j = 0; j < transform.GetChild(i).GetComponent<SupplyWpnBox>().m_iNumOfBuyingObj; ++j)
                        {
                            GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().ArmoryWeaponList.Add(transform.GetChild(i).GetComponent<SupplyWpnBox>().m_SupplyWpn.m_WpnData);
                            transform.GetChild(i).GetComponent<SupplyWpnBox>().m_SupplyWpn.m_iNumberOfWpn--;
                        }

                    }
                }
            }
            ResetPage();
            GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().g_iMoney -= m_iTotalPrice;
            GameObject.Find("ShopWdw(Clone)").GetComponent<ShopWdw>().SwipeToRightWdw();

            GameObject.Find("RightWdw").GetComponent<RightWdwMgr>().CurWinNum = 0;
        }
        else
        {
            GameObject WarnMsg = Resources.Load("Prefaps/UI/StaticUI/Warn/WarnMessageLabel") as GameObject;
            (Instantiate(WarnMsg) as GameObject).transform.parent = transform.parent;
        }
    }

    public void CalculateTotalPrice()
    {
        m_iTotalPrice = 0;

        for (int i = 0; i < transform.childCount; ++i)
        {
            if (transform.GetChild(i).gameObject.name == "SupplyWpnBox(Clone)")
            {
                m_iTotalPrice += transform.GetChild(i).GetComponent<SupplyWpnBox>().m_iThisPrice;
            }
        }

        transform.FindChild("TotalPrice").GetComponent<UILabel>().text = string.Format("$ {0}", m_iTotalPrice);
    }

    void WpnBox()
    {
        for (int i = 0; i < m_SupplyWpnGroup.Count + 1; ++i)
        {
            (Instantiate(Resources.Load("Prefaps/UI/Paper/Supply/SupplyWpnBox") as GameObject) as GameObject).transform.parent = transform;
            transform.GetChild(i+1).GetComponent<UISprite>().depth = GetComponent<UISprite>().depth + 1;
            transform.GetChild(i + 1).localPosition = new Vector3(-1.35f, 25 - (i * 4.5f));
            transform.GetChild(i + 1).localScale = new Vector3(0.85f, 0.05f);
            if(i != 0)
                transform.GetChild(i + 1).GetComponent<SupplyWpnBox>().m_SupplyWpn = m_SupplyWpnGroup[i - 1];
            else
                transform.GetChild(i + 1).GetComponent<SupplyWpnBox>().m_bExample = true;
            transform.GetChild(i + 1).GetComponent<SupplyWpnBox>().Init();
        }
    }

    void WpnRandomly()
    {
        List<int> iRepeatChecker = new List<int>();
        int iWpnRandomPeak;

        for (int i = 0; i < Random.Range(4,11); ++i)
        {
            WpnCategorize backWpnCategorize = new WpnCategorize();

            //겹치는 무기없게 체크↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
            while (true)
            {
                bool bRepeat = false;

                iWpnRandomPeak = Random.Range(0, 8);  //####주의### 이 Range가 위 for문보다 작으면 안댐 무한루프빠짐

                for (int j = 0; j < iRepeatChecker.Count; ++j)
                {
                    if (iRepeatChecker[j] == iWpnRandomPeak)
                        bRepeat = true;
                }

                if (bRepeat)
                    continue;

                iRepeatChecker.Add(iWpnRandomPeak);
                break;
            }
            ///////////////////////////↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑


            switch (iWpnRandomPeak)
            {
                case 0:
                    backWpnCategorize.m_WpnData = GameObject.Find("DataLogic").GetComponent<DataManager>().Get_WeaponData_List(WEAPON_TYPE.RIFLE, "AK47");
                    break;

                case 1:
                    backWpnCategorize.m_WpnData = GameObject.Find("DataLogic").GetComponent<DataManager>().Get_WeaponData_List(WEAPON_TYPE.PISTOL, "Tokarev");
                    break;

                case 2:
                    backWpnCategorize.m_WpnData = GameObject.Find("DataLogic").GetComponent<DataManager>().Get_WeaponData_List(WEAPON_TYPE.CLOSE, "Shield");
                    break;

                case 3:
                    backWpnCategorize.m_WpnData = GameObject.Find("DataLogic").GetComponent<DataManager>().Get_WeaponData_List(WEAPON_TYPE.RIFLE, "Bizon PP-19");
                    break;

                case 4:
                    backWpnCategorize.m_WpnData = GameObject.Find("DataLogic").GetComponent<DataManager>().Get_WeaponData_List(WEAPON_TYPE.RIFLE, "FN FAL");
                    break;

                case 5:
                    backWpnCategorize.m_WpnData = GameObject.Find("DataLogic").GetComponent<DataManager>().Get_WeaponData_List(WEAPON_TYPE.RIFLE, "PKM");
                    break;

                case 6:
                    backWpnCategorize.m_WpnData = GameObject.Find("DataLogic").GetComponent<DataManager>().Get_WeaponData_List(WEAPON_TYPE.RIFLE, "SVD");
                    break;

                case 7:
                    backWpnCategorize.m_WpnData = GameObject.Find("DataLogic").GetComponent<DataManager>().Get_WeaponData_List(WEAPON_TYPE.RIFLE, "Vintorez");
                    break;
            }

            backWpnCategorize.m_iNumberOfWpn = Random.Range(3, 15);

            m_SupplyWpnGroup.Add(backWpnCategorize);
        }
    }
}
